<?php

function login($username, $pwd) {
	if(strtolower($_GET['checkcode'])==strtolower($_SESSION['code'])){
		if(!empty($username)&&!empty($pwd)){
			$res=DB::fetch_first("SELECT * FROM %t WHERE stuid=%s",array('extra_vote',$username));
			if($res){
				if($res['stuname']==$pwd){
					return 1;
				}else{
					return -1;
				}
			}else{
				return -2;
			}
		}else{
			return -3;
		}
	}else{
		return -4;
	}
	
}

function getPolled() {
	$result=DB::fetch_all("SELECT teachid FROM %t WHERE stuid=%d",array('extra_votestat',$_SESSION['stuid']));
	$returnArray = array ();
	foreach($result as $val){
		$returnArray[$val['teachid']]=1;
	}
	return $returnArray;
}
function isPolled($votes,$polled){
	if(is_array($votes)){
		foreach($votes as $val){
			if($polled[$val]){
				return true;
			}
		}
		return false;
	}else{
		if($polled[$votes]){
			return true;
		}else{
			return false;
		}
	}
	
}
function getTotalPolled($name) {
	$query = "SELECT COUNT(*) FROM `voter` WHERE `stuid`=\"{$name}\"";
	$result = mysql_query ( $query );
	$row = mysql_fetch_array ($result);
	return $row [0];
}

function updatePoll($id, $name) {
	$query = "SELECT count(*) FROM `voter` WHERE `stuid`=\"{$name}\"";
	$result = mysql_query ( $query );
	$num = mysql_fetch_array ($result);
	if ($num [0] < 10) {
		$query = "SELECT count(*) FROM `voter` WHERE `stuid`=\"{$name}\" AND `id` = \"{$id}\"";
		$result = mysql_query ( $query );
		$num = mysql_fetch_array ($result);
		if ($num [0] == 0) {
			$query = "UPDATE `votes` SET `vote` = `vote` + 1 WHERE `_id` = \"{$id}\"";
			mysql_query ( $query );
			$query = "INSERT INTO `voter` (`stuid`, `id`) VALUES(\"{$name}\", \"{$id}\")";
			mysql_query ( $query );
			return "Success";
		} else
			return "Same poll";
	} else
		return "Max times";

}

function getMajor($collage, &$major) {
	$query = "SELECT * FROM `waws_majorcourses` WHERE `collage` = \"{$collage}\"";
	$result = mysql_query( $query );
	while ( $row = mysql_fetch_assoc ($result) ) {
		$major [] = $row;
	}
}
?>
